package pageTest.login;

import constants.HostConstant;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;
import utils.WebDriverUtils;

public class RegisterPage extends WebDriverUtils {
    private static final String REGISTER_PAGE_URL = "http://" + HostConstant.HOST + ":5173/user/register";

    public RegisterPage() {
        super(INIT_URL);
    }

    @BeforeMethod
    public void beforeEachTest() {
        driver.navigate().to(REGISTER_PAGE_URL);
    }

    @Test
    public void pageRight() {
        String topNavBarSlc = "#app > div:nth-child(1) > div > div > div > div.van-nav-bar__title.van-ellipsis";
        wait.until(ExpectedConditions.textToBe(By.cssSelector(topNavBarSlc), "注册"));
        wait.until(ExpectedConditions.textToBe(By.cssSelector("#van-field-1-label"), "账号"));
    }

    @Test
    public void registerFail() {
        // 只能输入数字、字母、下划线、连字符
        // 账号名长度不小于4，密码长度不小于8
        WebElement accountElem = driver.findElement(By.cssSelector("#van-field-1-input"));
        WebElement passwordElem = driver.findElement(By.cssSelector("#van-field-2-input"));
        WebElement confirmPawElem = driver.findElement(By.cssSelector("#van-field-3-input"));
        WebElement signupButton = driver.findElement(By.cssSelector("#login > form > div:nth-child(2) > button.van-button.van-button--primary.van-button--normal.van-button--block.van-button--round"));

        // 1. 账号不正确
        passwordElem.sendKeys("12345678");
        confirmPawElem.sendKeys("12345678");
        // 1) 账号为空
        actions.moveToElement(signupButton).click().perform();
        assert driver.findElement(By.cssSelector("#van-field-1-label")).getText().equals("账号");
        // 2) 账号包含特殊字符
        accountElem.sendKeys("哈哈");
        actions.moveToElement(signupButton).click().perform();
        assert driver.findElement(By.cssSelector("#van-field-1-label")).getText().equals("账号");
        // 3) 账号长度小于4
        inputClearAndSend(accountElem, "abc");
        actions.moveToElement(signupButton).click().perform();
        assert driver.findElement(By.cssSelector("#van-field-1-label")).getText().equals("账号");
        // 4) 账号长度过长
        inputClearAndSend(accountElem, "abcdefgdassssdassssssssssadsasd");
        actions.moveToElement(signupButton).click().perform();
        assert driver.findElement(By.cssSelector("#van-field-1-label")).getText().equals("账号");

        // 2. 密码不正确
        inputClearAndSend(accountElem, "autoTest");
        // 1) 密码为空
        inputClearAndSend(passwordElem, " ");
        actions.moveToElement(signupButton).click().perform();
        assert driver.findElement(By.cssSelector("#van-field-1-label")).getText().equals("账号");
        // 2) 密码包含特殊字符
        inputClearAndSend(passwordElem, "@@");
        inputClearAndSend(confirmPawElem, "@@");
        actions.moveToElement(signupButton).click().perform();
        assert driver.findElement(By.cssSelector("#van-field-1-label")).getText().equals("账号");
        // 3) 密码长度小于 8
        inputClearAndSend(passwordElem, "1234567");
        inputClearAndSend(confirmPawElem, "1234567");
        actions.moveToElement(signupButton).click().perform();
        assert driver.findElement(By.cssSelector("#van-field-1-label")).getText().equals("账号");
        // 4) 密码长度过长（大于 30）
        inputClearAndSend(passwordElem, "123456789asdasdddddddddas-------------------------");
        inputClearAndSend(confirmPawElem, "123456789asdasdddddddddas-------------------------");
        actions.moveToElement(signupButton).click().perform();
        assert driver.findElement(By.cssSelector("#van-field-1-label")).getText().equals("账号");
        // 5) 密码与确认密码不一致
        inputClearAndSend(passwordElem, "12345678");
        inputClearAndSend(confirmPawElem, "1234567");
        actions.moveToElement(signupButton).click().perform();
        assert driver.findElement(By.cssSelector("#van-field-1-label")).getText().equals("账号");
    }

    @Test
    public void registerSuccess() {
        WebElement accountElem = driver.findElement(By.cssSelector("#van-field-1-input"));
        WebElement passwordElem = driver.findElement(By.cssSelector("#van-field-2-input"));
        WebElement confirmPawElem = driver.findElement(By.cssSelector("#van-field-3-input"));
        WebElement signupButton = driver.findElement(By.cssSelector("#login > form > div:nth-child(2) > button.van-button.van-button--primary.van-button--normal.van-button--block.van-button--round"));
        inputClearAndSend(accountElem, "autoTest9");
        inputClearAndSend(passwordElem, "12345678");
        inputClearAndSend(confirmPawElem, "12345678");
        signupButton.click();
        // 跳转到标签选择页面
        wait.until(ExpectedConditions.textToBe(By.cssSelector("#afterRegister > div:nth-child(1) > div > div > div > div.van-nav-bar__title.van-ellipsis"), "选择标签"));
        String rightTopText = driver.findElement(By.cssSelector("#afterRegister > div:nth-child(1) > div > div > div > div.van-nav-bar__right.van-haptics-feedback")).getText();
        assert "跳过".equals(rightTopText);
        String someoneTagText = driver.findElement(By.cssSelector("#afterRegister > form > div.van-tree-select > div.van-tree-select__content > div:nth-child(2)")).getText();
        assert "女".equals(someoneTagText);
        // 点击“跳过”或“完成”跳转至 index 页面
        driver.findElement(By.cssSelector("#afterRegister > div:nth-child(1) > div > div > div > div.van-nav-bar__right.van-haptics-feedback")).click();
        wait.until(ExpectedConditions.textToBe(By.cssSelector("#app > div:nth-child(1) > div > div > div > div.van-nav-bar__title.van-ellipsis"), "CAMPUS 校园伙伴"));
    }


    @AfterClass
    public void tearDown() {
        driver.quit();
    }

}
